//
//  HLLLoginViewController.m
//  PhotoHLL
//
//  Created by mac on 2017/9/19.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "HLLLoginViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"
#import "HLLBarViewController.h"
#import "HLLCusTextField.h"
@interface HLLLoginViewController ()
 
@property (weak, nonatomic) IBOutlet HLLCusTextField *pwdView;
@property (weak, nonatomic) IBOutlet HLLCusTextField *userView;
 


@property (weak, nonatomic) IBOutlet UIView *videoView;
@property (strong, nonatomic)  AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPlayerItem *playerItem;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerViewController  *playerView;

/** 播放开始之前的图片 */
@property (nonatomic , strong)UIImageView *startPlayerImageView;
/** 播放中断时的图片 */
@property (nonatomic , strong)UIImageView *pausePlayerImageView;

@end

@implementation HLLLoginViewController

- (BOOL)shouldAutorotate {
    return NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBar.hidden = YES;
}
#pragma mark -- 初始化视频
- (void)prepareMovie {
    //首次运行
   // NSString *filePath = nil;
    //if (![self isFirstLauchApp]) {
        //第一次安装
     //   filePath = [[NSBundle mainBundle] pathForResource:@"opening_long_1080*1920.mp4" /ofType:nil];
     //   [self setIsFirstLauchApp:YES];
   // }else {
    //NSString *filePath = [[NSBundle mainBundle] pathForResource:@"openapp_vedio.mp4" ofType:nil];
   // }
    //初始化
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"openapp_vedio.mp4" ofType:nil];
    NSURL *url=[NSURL fileURLWithPath:filePath]; 
    
    self.player = [[AVPlayer alloc] init];
    self.playerItem = [[AVPlayerItem alloc] initWithURL:url];
    
     
    _player = [AVPlayer playerWithPlayerItem:self.playerItem];
    
    self.playerLayer = [[AVPlayerLayer alloc] init];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.frame = CGRectMake(0, 0, KWidth,  KHeight - 126);
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.videoView.layer addSublayer:self.playerLayer];
    
    //关闭AVPlayerViewController内部的约束
   // self.playerView.view.translatesAutoresizingMaskIntoConstraints = YES;
    //self.showsPlaybackControls = NO;
    //播放视频
   // [self.player play];
    
    //注册通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(runLoopTheMovie:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    

}
  
- (void)runLoopTheMovie:(NSNotification *)n{
    //注册的通知  可以自动把 AVPlayerItem 对象传过来，只要接收一下就OK
    
    AVPlayerItem * p = [n object];
    //关键代码 
    [p seekToTime:kCMTimeZero];
    
    [self.player play];
    
    NSLog(@"重播");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.userView setFrame:self.userView.frame];
    [self.pwdView setFrame:self.pwdView.frame];
    // 设置界面
    //[self setupView];
    //初始化视频
    [self prepareMovie];
    // Do any additional setup after loading the view.
}
#pragma mark -- 初始化视图逻辑
- (void)setupView {
   // self.startPlayerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lauch"]];
   // _startPlayerImageView.frame = CGRectMake(0, 0, KWidth, KHeight);
   // [self.contentOverlayView addSubview:_startPlayerImageView];
    //是否是第一次进入视频
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)junpTOMainView:(id)sender {
    [self enterMain];
}
//进入主界面
- (void)enterMain {
    [self.player pause];
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    HLLBarViewController *main = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
    delegate.window.rootViewController = main;
    [delegate.window makeKeyWindow];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
